//
//  ClubDetailViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/26.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "ClubDetailViewController.h"
//UMSocialUIDelegate
@interface ClubDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, retain) CLubDetailHeaderView *viewHeader;

@property (nonatomic, retain) ClubModel *modelHeader;
@property (nonatomic, retain) NSMutableArray *arrComment;

@property (nonatomic, assign) NSInteger page;/**< 用来保存请求新数据动态值 */

@end

@implementation ClubDetailViewController

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    [_tableView release];
    [_viewHeader release];
    [_arrComment release];
    [_modelHeader release];
    [_modelDetail release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"话题详情";
    
    //分享
 // self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"zhuanfa"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(share:)] autorelease];
    
    self.arrComment = [NSMutableArray array];
    [self dataTop];
    [self dataHandler];
    [self createTableView];
    [self refresh];
   
}


#pragma mark - ------------ 数据处理 ---------------
- (void)dataTop
{
    
    //头部数据
     NSString *strDetail = @"http://203.80.144.212/napi/topic/detail/?locale=zh_CN&include_fields=share_links&screen_width=320&screen_height=568&platform_version=9.3.1&app_code=gandalf&platform_name=iPhone%20OS&device_platform=iPhone5%2C2&__domain=www.duitang.com&device_name=iPhone%205&app_version=6.0.6%20rv%3A156728&topic_id=240656";
    
     NSString *detailURL = [strDetail stringByReplacingOccurrencesOfString:@"240656" withString:_modelDetail.selfID.stringValue];
    
    [XrDataParse GETWithURL:detailURL par:nil success:^(id responseObject) {

        self.modelHeader = [[ClubModel alloc]init];

       NSDictionary *dic = responseObject[@"data"];
       [_modelHeader setValuesForKeysWithDictionary:dic];
        
        [self createHeaderView];
        [_tableView reloadData];
        
       [_modelHeader release];
       
        
    } filed:^(NSError *error) {
        
    }];
    
}


#pragma mark - ------------ 下拉刷新 上拉加载 ---------------

- (void)refresh
{
    //刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 0;
        if (_page == 0) {
            [_arrComment removeAllObjects];
        }
        [self dataHandler];
        [_tableView.mj_header endRefreshing];
    }];
    
    //加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
      _page += 25;
       [self dataHandler];
        [_tableView.mj_footer endRefreshing];
    }];
    
}

- (void)dataHandler
{

    
    //tableview数据
    NSString *strComment = @"http://203.80.144.212/napi/topic/comment/list/?locale=zh_CN&limit=0&screen_height=568&screen_width=320&start=0&platform_version=9.3.1&app_code=gandalf&device_platform=iPhone5%2C2&platform_name=iPhone%20OS&__domain=www.duitang.com&app_version=6.0.6%20rv%3A156728&topic_id=240656&device_name=iPhone%205";
    
    NSString *commentURL= [strComment stringByReplacingOccurrencesOfString:@"240656" withString:_modelDetail.selfID.stringValue];
    
    NSString *url = [commentURL stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%ld", _page]];
    
    [XrDataParse GETWithURL:url par:nil success:^(id responseObject) {
        
        NSArray *arrComment = responseObject[@"data"][@"object_list"];
        NSArray *arr = [ClubModel modelHandleWithArrar:arrComment];
        [_arrComment addObjectsFromArray:arr];
        
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
}


#pragma mark - ------------ 头视图 ---------------
- (void)createHeaderView
{
   
    CGFloat h = [CLubDetailTableViewCell heightWithText:_modelHeader.content];
    self.viewHeader = [[CLubDetailHeaderView alloc]init];
    _viewHeader.model = _modelHeader;
   _viewHeader.dk_backgroundColorPicker = DKColorWithColors(GrayLight,[UIColor lightGrayColor]);
    /**< 判断 返回不同的高度  */
    if (_modelHeader.path != nil) {
       _viewHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, h + 360);
    }else{
    
        _viewHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, h + 210);
    }
    self.tableView.tableHeaderView = _viewHeader;
    [_viewHeader release];
}

#pragma mark - ------------ 创建Tableview ---------------
- (void)createTableView
{
    
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //隐藏分割线
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[CLubDetailTableViewCell class] forCellReuseIdentifier:@"pool"];
/** 夜间 */
  _tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor grayColor]);
    [_tableView release];
}


#pragma mark - ------------ 返回个数 ---------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _arrComment.count;
}

#pragma mark - ------------ cell赋值 ---------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLubDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_arrComment.count != 0) {
        
        ClubModel *m = _arrComment[indexPath.row];
        cell.model = m;
    }
    
    return cell;
}

#pragma mark - ------------ 返回高度 ---------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_arrComment.count != 0) {
    
    ClubModel *m = _arrComment[indexPath.row];
    
    CGFloat h = [CLubDetailTableViewCell heightWithText:m.content];
    
    CGFloat h1 = [CLubDetailTableViewCell heightWithText:[[m.replayUsername stringByAppendingString:@":   "] stringByAppendingString:m.replayContent]];

   if (m.path != nil && m.replayContent != nil) {
    
      return h + h1 + 326;
    
     }else if (m.path == nil && m.replayContent == nil){
     
     
         return h + 115;
     
     }else if (m.path != nil && m.replayContent == nil){
         
         return h + 245;
         
         
     }else if (m.path == nil && m.replayContent != nil){
        
         return h + h1 + 196;
         
         
     }
   }
    return 0;
    
    
}

#if 0
#pragma mark - ------------ 分享到微博 ---------------
- (void)share:(UIBarButtonItem *)item
{
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"572af9b767e58e632b000f71"
     
                                      shareText:@"分享"
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,UMShareToRenren,UMShareToQzone,UMShareToAlipaySession,UMShareToFacebook,nil]
                                       delegate:self];
}

#endif

#pragma mark - ------------ 将要结束拖拽 当滑动速度>0 导航栏隐藏 <0出现 ---------------
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    
    if (velocity.y > 0) {
        self.navigationController.navigationBar.hidden = YES;
        self.tabBarController.tabBar.hidden = YES;
        
    }else{
        
        self.navigationController.navigationBar.hidden = NO;
        self.tabBarController.tabBar.hidden = NO;
        
    }
}

#pragma mark - ------------ scrollview 最后走的方法 滑动减速停止 导航栏出现 ---------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
